from flask import Blueprint,jsonify,request
from db import db1
from utils.redisconn import r
import json


bp_work = Blueprint('workf',__name__)


@bp_work.route('/workflow')
def workflow():
    sql = "select * from workf"
    res = db1.find_all(sql)
    return jsonify({'code':200,'list':res})


#获取数据源接口
@bp_work.route('/getresourcedata')
def getresourcedata():
    #从redis取，如果有直接返回
    values = r.get_str('dataresouce')
    if values:
        list = eval(values)
    else:
        #如果没有查询
        sql = "select * from data_source"
        res = db1.find_all(sql)
        list = []
        if res:
            for i in res:
                dict={'id':i['id'],'name':i['name']}
                sql1 = "select id,name,type from %s"%(i['tablename'])
                subres = db1.find_all(sql1)
                dict['mes'] = subres
                list.append(dict)
        #存入redis
        r.set_str('dataresouce',json.dumps(list))
    return jsonify({"code":200,'list':list})


@bp_work.route('/updateworkf',methods=['POST','GET'])
def updateworkf():
    if request.method == 'POST':
        res = request.json
        sql = "update workf set params='%s' where id=%d"%(res['params'],int(res['id']))
        db1.update(sql)
        return jsonify({"code":200,'msg':'成功'})
    else:
        pid = request.args.get('id')
        sql = "select * from workf where id=%d"% int(pid)
        res = db1.find(sql)
        return jsonify({'code':200,'list':res})
# [{"lable":"姓名","type":"input","values":"[{"id":1,"name":"事假","type":1},{"id":2,"name":"产假","type":2},{"id":3,"name":"病假","type":3}]"},{"lable":"工号","type":"input"}]
